﻿package td.towers
{
	import flash.display.Graphics;
	import flash.display.Sprite;
	import flash.display.MovieClip;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.events.TimerEvent;
	import flash.utils.Timer;
	
	import td.*;
	import td.projectiles.*;
	import td.Global.GlobalUser;

	/**
	 * The tower class that should be extended for every type of tower
	 * 
	 * @author Garth Henson - Guahan Web (garth@guahanweb.com)
	 */
	public class Tower extends Sprite
	{
		protected var game :MagicTD;
		protected var timer :Timer;
		
		public var price:Number;//购买价格
		public var dead:Number;//致命伤害比例
		public var deadpro:Number;//致命几率
		public var type :String;//塔类型，库内导出名
		public var power :int;//攻击力
		public var range :int;//攻击范围
		public var actRange:int;//溅射范围
		public var actNum:int;//群攻数
		public var rate :int; // shots per second
		public var level :int;
		public var is_set :Boolean = false;
		
		protected var rangeMC :Sprite;
		private var tip:MovieClip;
		public var towerMC :Sprite;
		
		/**
		 * Constructor method
		 * 
		 * @constructor
		 * @access public
		 * @param {MagicTD} mtd A reference to the current TD game
		 * @return Tower
		 */
		public function Tower(mtd :MagicTD,_type:String=null)
		{
			// Set up the defaults
			if (this.type == null) this.type = 'tower_8';
			if (this.power == 0) this.power = 4;			
			if (this.range == 0) this.range = 80;
			if (this.rate == 0) this.rate = 2;
			if (this.level == 0) this.level = 1;
			if (this.actNum == 0) this.actNum = 1;
			
			this.game = mtd;
			
			drawRange();
			drawTower();
			//towerTip();
			
			this.towerMC.buttonMode = true;
			this.towerMC.addEventListener(MouseEvent.CLICK, select);
			this.game.addEventListener(MouseEvent.MOUSE_MOVE, trackMouse);
			
			super();
		}
		
		/**
		 * Generates the display for this tower
		 * 
		 * @access protected
		 * @return void
		 */
		protected function drawTower() :void
		{
			//this.towerMC = new Sprite();
			//var g :Graphics = this.towerMC.graphics;
			//g.lineStyle(1, 0x454545);
			//g.beginFill(0xaaaaaa);
			//g.drawRect(-6, -6, 12, 12);
			//g.endFill();
			
			var _myClass:Class = game.towerlibs.contentLoaderInfo.applicationDomain.getDefinition(type) as Class;
			this.towerMC = new _myClass as MovieClip;
			this.addChild(this.towerMC);
		}
		
		/**
		 * "Selects" this tower visually
		 * 
		 * @param {MouseEvent} e the CLICK event
		 * @return void
		 */
		protected function select(e :MouseEvent) :void
		{
			if (this.is_set == false && x<700 && y<560)
			{
				this.is_set = true;
				this.game.removeEventListener(MouseEvent.MOUSE_MOVE, trackMouse);
				this.setupTimer();
			}
			trace("isClicked")
			//tip.visible = true;
			//tip.levelup_txt.text = "下一级所需金币：" + this.price * this.level * 3;
			this.rangeMC.visible = true;
			game.clickTower = this;
		}
		
		/**
		 * "Deselects" this tower visually
		 * 
		 * @return void
		 */
		public function deselect() :void
		{
			this.rangeMC.visible = false;
		}
		
		protected function trackMouse(e :MouseEvent) :void
		{
			var x :int = Math.ceil(this.parent.mouseX / this.game.grid_size);
			var y :int = Math.ceil(this.parent.mouseY / this.game.grid_size);
			
			this.x = this.game.getPointToPixels(x, true);
			this.y = this.game.getPointToPixels(y, true);
		}
		
		/**
		 * Draws the range circle
		 * 
		 * @return void
		 */
		protected function drawRange() :void
		{
			if (rangeMC) removeChild(rangeMC);
			this.rangeMC = new Sprite();
			var g :Graphics = this.rangeMC.graphics;
			g.lineStyle(1, 0x333333, 0.4);
			g.beginFill(0xaa0000, 0.1);
			g.drawCircle(0, 0, this.range);
			g.endFill();

			this.addChildAt(this.rangeMC,0);
		}
		
		/**
		 * 升级提示框
		 */
		protected function towerTip():void {
				if (GlobalUser.userData.nUMoney < price * level * 3) return;
				level++;
				power += level/10*power;
				rate += level/10*rate;
				range += level/10*range;
				drawRange();//重新绘制rangeMC
				dispatchEvent(new Event("levelup"));
		}
		
		/**
		 * Sets the shot timer for this tower
		 * 
		 * @access protected
		 * @return void
		 */
		protected function setupTimer() :void
		{
			var milli :int = Math.round((1 / this.rate) * 1000);
			this.timer = new Timer(milli);
			this.timer.addEventListener(TimerEvent.TIMER, fire);
			this.timer.start();
		}
		
		/**
		 * Triggers the MagicTD.fire() method with this tower as the target object
		 * 
		 * @access protected
		 * @param {TimerEvent} e The fire timer event
		 * @return void
		 */
		protected function fire(e :TimerEvent) :void
		{
			this.game.fire(this);
		}
		
		/**
		 * Creates a new bullet to be fired from this tower
		 * 
		 * @access public
		 * @param {Creep} t The target creep
		 * @return Bullet
		 */
		public function getNewBullet(t :Creep) :Bullet
		{
			var b :Bullet = new Bullet(t);
			b.x = this.x;
			b.y = this.y;
			b.setPower(this.power);
			return b;
		}
		
		/**
		 * Accessor method to retrieve the range of this tower
		 * 
		 * @access public
		 * @return int
		 */
		public function getRange() :int
		{
			return this.range;
		}
	}
}